package cn.fl.ref.fncpreabslimrec.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.fncpreabslimrec.service.IFncPreAbsLimRecService;
import cn.fl.ref.fncpreabslimrec.transform.FncPreAbsLimRecVOTransform;
import cn.fl.ref.fncpreabslimrec.transform.FncPreAbsPayRVOTransform;
import cn.fl.ref.fncpreabslimrec.vo.FncPreAbsLimRecVO;
import cn.fl.ref.fncpreabslimrec.vo.FncPreAbsPayRVO;
import fl.ref.fncpreabslim.dto.FncPreAbsLimDTO;
import fl.ref.fncpreabslim.service.IFncPreAbsLimApiService;
import fl.ref.fncpreabslimrec.condition.FncPreAbsLimRecQueryCondition;
import fl.ref.fncpreabslimrec.dto.FncPreAbsLimRecDTO;
import fl.ref.fncpreabslimrec.service.IFncPreAbsLimRecApiService;
import fl.ref.fncpreabspayr.dto.FncPreAbsPayRDTO;
import fl.ref.fncpreabspayr.service.IFncPreAbsPayRApiService;
/**
 * @Description: 融资机构PRE-ABS额度使用记录表
 * @Description:业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 * 服务实现层
 */
@Service
public class FncPreAbsLimRecServiceImpl implements IFncPreAbsLimRecService {

	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(IFncPreAbsLimRecService.class);
	
	@Autowired
	private IFncPreAbsLimRecApiService  fncPreAbsLimRecApiService;
	
	@Autowired
	private IFncPreAbsLimApiService  fncPreAbsLimApiService;
	
	@Autowired
	private IFncPreAbsPayRApiService fncPreAbsPayRApiService;
	
	@Override
	public FncPreAbsLimRecVO selectById(Long id) throws BaseException {
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		FncPreAbsLimRecDTO dto = this.fncPreAbsLimRecApiService.selectById(id);
		return FncPreAbsLimRecVOTransform.toVO(dto);
	}

	@Override
	public Long add(FncPreAbsLimRecVO entity) throws BaseException {
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		if(entity.getLimId()==null){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"额度编号不能为空");	
		}
		if(entity.getBatNo()==null){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"批次号不能为空");	
		}
		if(entity.getLimTypCd()==null){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"额度变化类型不能为空");	
		}
		if(entity.getLimOpAmt()==null){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"额度变化金额不能为空");	
		}
		FncPreAbsLimDTO absdto=this.fncPreAbsLimApiService.selectById(entity.getLimId());
		if(null==absdto){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"未找到该额度信息");		
		}
		//系统日志-有用户的情况下
		BaseLogUtils.info(logger, "add", "新增FncPreAbsLimRec数据.", CurrentThreadContext.getCurrentUserName());
		
		//系统日志-无用户的情况下
		BaseLogUtils.info(logger, "add", "新增FncPreAbsLimRec数据.");
		
		if(entity.getLimTypCd().equals("1")|| entity.getLimTypCd().equals("3")){
			//if(absdto.getCurrLimAmt()-entity.getLimOpAmt()<0){
			//throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"剩余额度不足");	
			//}
		
		}
		if(entity.getLimTypCd().equals("2")|| entity.getLimTypCd().equals("4")){
			if(absdto.getCurrLimAmt()+entity.getLimOpAmt()>absdto.getPreLimAmt()){
			//throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"剩余额度不能超过总额度");	
			}	
		}
		Long id =this.fncPreAbsLimRecApiService.add(FncPreAbsLimRecVOTransform.toDTO(entity));
		return id;
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.fncPreAbsLimRecApiService.removeById(id);
	}

	@Override
	public boolean modify(FncPreAbsLimRecVO entity) throws BaseException {
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.fncPreAbsLimRecApiService.modify(FncPreAbsLimRecVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		if(null == page)page = new Pager();
		Pager pager=this.fncPreAbsLimRecApiService.searchListPage(page);
		List<FncPreAbsLimRecDTO> list = (List<FncPreAbsLimRecDTO>) pager.getResultList();
		List<FncPreAbsLimRecVO> volist=FncPreAbsLimRecVOTransform.toVOList(list);
		pager.setResultList(volist);
		return pager;
	}
	

	@Override
	public List<FncPreAbsLimRecVO> searchList(FncPreAbsLimRecQueryCondition condition) {
		// TODO Auto-generated method stub
		return FncPreAbsLimRecVOTransform.toVOList(this.fncPreAbsLimRecApiService.searchList(condition))  ;
	}	

	@Override
	public Pager searchAbsPayRListPage(Pager page) throws BaseException {
		if(null == page)page = new Pager();
		Pager pager=this.fncPreAbsPayRApiService.searchListPage(page);
		List<FncPreAbsPayRDTO> list = (List<FncPreAbsPayRDTO>) pager.getResultList();
		List<FncPreAbsPayRVO> volist=FncPreAbsPayRVOTransform.toVOList(list);
		pager.setResultList(volist);
		return pager;
	}

}
